package com.estate.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.estate.business.ForumService;
import com.estate.dao.CommentDao;
import com.estate.dao.ForumDao;
import com.estate.struts.actionForms.ForumDetailActionForm;
import com.estate.utils.ForumUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.ForumCommentVo;
import com.estate.vo.impl.ForumVo;
import com.estate.vo.impl.UserInfoVo;

public class ForumServiceImpl implements ForumService {

	private ForumDao forumDao;
	private CommentDao commentDao;

	public boolean addForum(ForumVo forum) {
		boolean result = false;
		result = this.getForumDao().addVo(forum);
		return result;
	}

	public boolean deleteForum(ForumVo forum) {
		boolean result = false;
		List<ForumCommentVo> commentList = this.getCommentDao().getForumCommentList(forum.getId());
		this.getCommentDao().deleteForumComments(commentList);
		result = this.getForumDao().deleteVo(forum);
		return result;
	}

	public boolean deleteMultiForum(String delStr) {
		if(delStr!= null && !"".equals(delStr) && !",".equals(delStr)){
			String[] delArray = delStr.split(",");
			ForumVo forum = null;
			for(int i = 0;i<delArray.length;i++){
				forum = new ForumVo();
				forum.setId(Integer.parseInt(delArray[i]));
				this.deleteForum(forum);
			}
		}
		return true;
	}

	public List<Vo> getMyForumList(UserInfoVo userInfo) {
		List<Vo> myForumList = this.getForumDao().findAllMyForumList(userInfo);
		return myForumList;
	}

	public Vo getForumVo(int id) {
		
		ForumVo vo = (ForumVo) this.getForumDao().findByPK(id);
		return vo;
	}

	public boolean updateForum(ForumVo forum) {
		ForumVo old = (ForumVo) this.getForumDao().findByPK(forum.getId());
		forum.setPostUser(old.getPostUser());
		boolean result = false;
		if(old.getState().equals("未发布")){
			if("已发布".equals(forum.getState())){
				forum.setPostDate(new Date());
				forum.setReadNum(0);
			}
		}else{
			forum.setPostDate(old.getPostDate());
			forum.setReadNum(old.getReadNum());
		}
		result = this.getForumDao().updateVo(forum);
		
		return result;
	}

	public boolean updatePost(int id) {
		ForumVo old = (ForumVo) this.getForumDao().findByPK(id);
		old.setState(ForumUtil.getForumString(2));
		old.setPostDate(new Date());
		boolean result = this.getForumDao().updateVo(old);
		return result;
	}

	public boolean updateReadNum(ForumVo vo) {
		if(vo.getState().equals("已发布")){
			int redNum = vo.getReadNum()+1;
			vo.setReadNum(redNum);
		}
		this.getForumDao().updateVo(vo);
		return true;
	}

	public List<Vo> getAllPostedForumList() {
		List<Vo> voList = this.getForumDao().findAllPostedForum();
		return voList;
	}

	public CommentDao getCommentDao() {
		return commentDao;
	}

	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}
	public ForumDao getForumDao() {
		return forumDao;
	}

	public void setForumDao(ForumDao forumDao) {
		this.forumDao = forumDao;
	}

	public boolean addComment(ForumCommentVo comment) {
		boolean result = false;
		result = this.getCommentDao().addVo(comment);
		
		ForumVo forum =(ForumVo) this.getForumVo(comment.getForumId()); 
		
		int commentNum = forum.getCommentNum();
		forum.setCommentNum((commentNum+1));
		forum.setLastCommentDate(comment.getCommentDate());
		forum.setLastCommentUser(comment.getCommentUser());
		
		return result;
	}

	public boolean updateComment(ForumCommentVo comment) {
		ForumCommentVo old = (ForumCommentVo) this.getCommentDao().findByPK(comment.getId());
		
		comment.setCommentUser(old.getCommentUser());
		comment.setForumId(old.getForumId());
		comment.setReplyId(old.getReplyId());
		comment.setCommentDate(old.getCommentDate());
		
		boolean result = false;
		result = this.getCommentDao().updateVo(comment);
		return result;
	}

	public Vo getComment(int pk) {
		Vo vo = this.getCommentDao().findByPK(pk);
		return vo;
	}

	public boolean deleteComment(ForumCommentVo comment) {
		boolean result = false;
		result = this.getCommentDao().deleteVo(comment);
		return result;
	}

	public ForumDetailActionForm getForumActionForm(int id) {
		ForumDetailActionForm fda = new ForumDetailActionForm();
		
		ForumVo vo = (ForumVo) this.getForumDao().findByPK(id);
		fda.setForum(vo);
		
		List<ForumCommentVo> commentList = this.getCommentDao().getForumCommentList(vo.getId());
		fda.setCommentList(commentList);
		
		Map<String,ForumCommentVo> commentMap = new HashMap<String,ForumCommentVo>();
		ForumCommentVo fcv = null;
		if(commentList != null && commentList.size()>0){
			for(int i = 0;i<commentList.size();i++){
				fcv = commentList.get(i);
				fcv.setFloor((i+1)+"楼");
				commentMap.put(fcv.getId()+"",fcv);
			}
			for(int i = 0;i<commentList.size();i++){
				fcv = commentList.get(i);
				getCommentTitle(commentMap,fcv,vo);
				System.out.println(fcv.getFloor()+fcv.getTitle());
			}
		}
		
		return fda;
	}

	private void getCommentTitle(Map<String,ForumCommentVo> commentMap,ForumCommentVo fvc,ForumVo forum){
		if(fvc == null){
			return;
		}
		int replyId = fvc.getReplyId();
		ForumCommentVo replyComment = commentMap.get(""+replyId);
//		fvc.setCommentTitle(fvc.getFloor()+fvc.getCommentUser().getLoginName()+"回复");
		if(replyComment == null){
			if(replyId == 0){
				fvc.setCommentTitle(fvc.getFloor()+fvc.getCommentUser().getLoginName()+" 回复楼主");
				fvc.setTitle("");
			}else{
				fvc.setCommentTitle(fvc.getFloor()+fvc.getCommentUser().getLoginName()+"回复");
				fvc.setTitle("");
			}
			return;
		}else{
			fvc.setCommentTitle(fvc.getFloor()+fvc.getCommentUser().getLoginName()+"回复"+replyComment.getFloor());
		}
		if(replyComment.getReplyId() != 0){
				if(replyComment.getTitle() == null || "".equals(replyComment.getTitle())){
					getCommentTitle(commentMap,replyComment,forum);
				}
				System.out.println("enter");
				
//				fvc.setCommentTitle(fvc.getFloor()+fvc.getCommentUser().getLoginName()+"回复"+replyComment.getFloor());
//				commentMap.get(replyComment.getReplyId()).getFloor();
//				System.out.println(replyComment.getTitle());
//				System.out.println(replyComment.getFloor());
//				System.out.println(replyComment.getCommentUser().getLoginName());
//				System.out.println(commentMap.get(replyComment.getReplyId()+"").getFloor());
				if(commentMap.get(replyComment.getReplyId()+"")!= null){
					String title = replyComment.getTitle()+"<br/>"+replyComment.getFloor()+replyComment.getCommentUser().getLoginName()+" 回复"+commentMap.get(replyComment.getReplyId()+"").getFloor()+"：<br/><div style='margin-left:20px;'>"+replyComment.getContent()+"</div>";
					fvc.setTitle(title);
				}else{
					String title = replyComment.getTitle()+"<br/>"+replyComment.getFloor()+replyComment.getCommentUser().getLoginName()+" 回复"+"：<br/><div style='margin-left:20px;'>"+replyComment.getContent()+"</div>";
					fvc.setTitle(title);
				}
		}else{
			if(replyComment != null){
//				fvc.setCommentTitle(fvc.getFloor()+fvc.getCommentUser().getLoginName()+" 回复楼主");
//				System.out.println(replyComment.getId());
				String title = replyComment.getFloor()+replyComment.getCommentUser().getLoginName()+" 回复"+"楼主：<br/><div style='margin-left:20px;'>"+replyComment.getContent()+"</div>";
				fvc.setTitle(title);
			}
//			fvc.setTitle(fvc.getCommentTitle()+"：<br/><div style='margin-left:20px;'>"+forum.getContent()+"</div>");
//			System.out.println("3");
		}
	}

	public List<ForumVo> getMyJoinForum(UserInfoVo userInfo) {
		List<ForumCommentVo> commentList = this.getCommentDao().getUserCommentList(userInfo);
		List<ForumVo> forumList =null;
		if(commentList != null && commentList.size()>0){
			Map<String,ForumCommentVo> commentMap = new HashMap<String,ForumCommentVo>();
			forumList = new ArrayList<ForumVo>();
			ForumCommentVo comment = null;
			for(int i = 0;i<commentList.size();i++){
				comment = commentList.get(i);
				if(!commentMap.containsKey(comment.getForumId()+"")){
					commentMap.put(comment.getForumId()+"",comment);
					forumList.add((ForumVo) this.getForumVo(comment.getForumId()));
				}
			}
		}
		
		return forumList;
	}
}
